package com.zhihuichongcheng.application.ui.main.fragment;

import android.app.DownloadManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseFragment;
import com.zhihuichongcheng.application.bean.openclass.OpenClassBean;
import com.zhihuichongcheng.application.bean.openclass.OpenClassCategoryBean;
import com.zhihuichongcheng.application.bean.openclass.SubCategory;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.mine.UserInfoActivity;
import com.zhihuichongcheng.application.ui.openclass.activity.KnowledgeWorldActivity;
import com.zhihuichongcheng.application.ui.openclass.activity.OpenClassDetailActivity;
import com.zhihuichongcheng.application.ui.openclass.activity.OpenClassListActivity;
import com.zhihuichongcheng.application.ui.openclass.activity.OpenclassSearchActivity;
import com.zhihuichongcheng.application.ui.openclass.adapter.OpenCLassAdapter;
import com.zhihuichongcheng.application.ui.openclass.adapter.OpenClassCategoryAdapter;
import com.zhihuichongcheng.application.util.FullyLinearLayoutManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 公开课fragment Created by 3water on 2016/10/27.
 */
public class OpenClassFragment extends BaseFragment implements View.OnClickListener {
    private ImageView mImUser, mImDownload, mImSearch;
    private XRecyclerView mRcvOpenCLass;
    private RecyclerView mRcvOpenClassCategory;
    private OpenCLassAdapter mAdapter = null;
    private List<OpenClassBean> mListData = new ArrayList<>();
    private OpenClassCategoryAdapter mOpenClassCategoryAdapter = null;
    private int mPageSize = 10;
    private int mPage = 1;

    @Override
    protected void init() {
        super.init();
        initView();
        initData();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.openclass_headview, null);
        mImSearch = (ImageView) view.findViewById(R.id.im_search);
        mImUser = (ImageView) mView.findViewById(R.id.im_user);
        mImDownload = (ImageView) mView.findViewById(R.id.im_download);
        mImUser.setOnClickListener(this);
        mImDownload.setOnClickListener(this);
        mImSearch.setOnClickListener(this);
        mRcvOpenClassCategory = (RecyclerView) view.findViewById(R.id.rcv_openclass_category);
        mOpenClassCategoryAdapter = new OpenClassCategoryAdapter(mContext);
        mOpenClassCategoryAdapter.setOnItemOnclick(new OnItemOnclick<OpenClassCategoryBean>() {
            @Override
            public void onClick(OpenClassCategoryBean openClassCategoryBean, int position) {
                final ArrayList<SubCategory> list = (ArrayList<SubCategory>) openClassCategoryBean.getSubCategories();
                if (list != null && list.size() > 0) {
                    Intent intent = new Intent(mContext, KnowledgeWorldActivity.class);
                    intent.putExtra("allCategoryId", openClassCategoryBean.getOpenClassCategoryId());
                    intent.putExtra("allCategoryId", openClassCategoryBean.getOpenClassCategoryId());
                    intent.putExtra("title", openClassCategoryBean.getCategoryName());
                    intent.putParcelableArrayListExtra("list", list);
                    startActivityByIntent(intent);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("allCategoryId", openClassCategoryBean.getOpenClassCategoryId());
                    bundle.putString("title", openClassCategoryBean.getCategoryName());
                    startActivity(OpenClassListActivity.class, bundle);
                }
            }
        });
        setTitle("公开课");
        mRcvOpenCLass = (XRecyclerView) mView.findViewById(R.id.rcv_openCLass);
        mRcvOpenCLass.addHeaderView(view);
        mAdapter = new OpenCLassAdapter(mContext);
        mAdapter.setOnItemOnclick(new OnItemOnclick<OpenClassBean>() {
            @Override
            public void onClick(OpenClassBean openClassBean, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("title", openClassBean.getTitle());
                bundle.putString("id", openClassBean.getOpenClassId());
                startActivity(OpenClassDetailActivity.class, bundle);
            }
        });
        mRcvOpenCLass.setLayoutManager(new LinearLayoutManager(mContext));
        mRcvOpenCLass.setAdapter(mAdapter);
        mRcvOpenCLass.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPage = 1;
                loadNewsList(mPage);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                loadNewsList(mPage);
            }
        });

    }

    private void initData() {
        mPage = 1;
        loadCategory();
        loadNewsList(mPage);
    }

    private void loadNewsList(final int page) {
        HashMap<String, String> params = new HashMap<>();
        params.put("token", mUser.getToken());
        params.put("pageRows", "" + mPageSize);
        params.put("curPage", "" + page);
        manager.post(NetworkAPI.Openclass_list, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                mListData = GsonUtil.paraeFromStringToList(value, OpenClassBean.class);
                if (page == 1) {
                    mAdapter.clear();
                }
                mAdapter.setData(mListData);
                mAdapter.notifyDataSetChanged();
                mRcvOpenCLass.loadMoreComplete();
                mRcvOpenCLass.refreshComplete();
                if (mListData.size() < mPageSize) {
                    mRcvOpenCLass.setNoMore(true);
                }
            }

            @Override
            public void onFailure(String value) {

            }
        }, params);
    }

    private void loadCategory() {
        HashMap<String, String> params = new HashMap<>();
        params.put("token", mUser.getToken());
        manager.post(NetworkAPI.OpenclassCategory, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                List<OpenClassCategoryBean> list = GsonUtil.paraeFromStringToList(value, OpenClassCategoryBean.class);
                FullyLinearLayoutManager manager = new FullyLinearLayoutManager(mContext);
                manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                mRcvOpenClassCategory.setLayoutManager(manager);
                mOpenClassCategoryAdapter.setData(list);
                mOpenClassCategoryAdapter.notifyDataSetChanged();
                mRcvOpenClassCategory.setAdapter(mOpenClassCategoryAdapter);
            }

            @Override
            public void onFailure(String value) {

            }
        }, params);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_open_class;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.im_user:
                startActivity(UserInfoActivity.class, null);
                break;
            case R.id.im_download:
                startActivityByIntent(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
                break;
            case R.id.im_search:
                startActivity(OpenclassSearchActivity.class, null);
                break;
            default:
                break;
        }
    }
}
